
export function exportXls(url){

  let link = document.createElement("a");
  //创建一个a标签
  link.style.display = "none";
  //将a标签隐藏

  link.href = url;
  //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
  link.setAttribute("download", name);
  // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
  document.body.appendChild(link);
  //将上面创建的a标签加入到body的尾部
  link.click();
}

export function toParams(param) {
  var result = ""
  for (let name in param) {
    if (typeof param[name] != 'function') {
      result += "&" + name + "=" + encodeURI(param[name]);
    }
  }
  return result.substring(1)
}


/**
 * 获取当月的日期集合(yyyy-MM-dd)
 * endIsCurrentDay true:截止日至为当前天  false:该月最后一天
 */
export function currentMonthDays(endIsCurrentDay=false) {
  // 获取标准时间
  const date = new Date();
  // 获取当天日期
  const currentDay = date.getDate();
  // 获取当前月份（实际月份需要加1）
  const currentMonth = date.getMonth() + 1 < 10 ? '0' + date.getMonth() + 1 : date.getMonth() + 1;
  // 获取当前年份
  const currentYear = date.getFullYear();
  // 获取当前月有多少天
  const currentMonthDays = new Date(currentYear, currentMonth, 0).getDate();
  // 当前月份所有日期集合
  const currentMonthArr = [];
  let endDay = "";
  if(endIsCurrentDay){
    endDay = currentDay
  }else{
    endDay = currentMonthDays
  }

  for (let day = 1; day <= currentMonthDays; day++) {
    if (day <= endDay) {
      // 年月日(yyyy-MM-dd)
      let dateItem = currentYear + "-" + currentMonth + "-" + (day < 10 ? '0' + day : day)
      currentMonthArr.push(dateItem)
    }
  }
  return currentMonthArr;
}

export function getDateStr(AddDayCount) {
  var dd = new Date();
  dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
  var y = dd.getFullYear();
  var m = dd.getMonth()+1;//获取当前月份的日期
  var d = dd.getDate();
  return y+"-"+m+"-"+d;
}

export function saveBas64Img (base64String, name) {
  let base64 = base64String.toString()
  let byteCharacters = atob(
    base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, "")
  )
  let byteNumbers = new Array(byteCharacters.length)
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i)
  }
  let byteArray = new Uint8Array(byteNumbers)
  let blob = new Blob([byteArray], {
    type: undefined
  })
  let aLink = document.createElement("a")
  aLink.download = `${name}.png` //这里写保存时的图片名称
  aLink.href = URL.createObjectURL(blob)
  aLink.click()
}


/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}


